

#include "MrpcApplication.h"
#include <cstdlib>
#include <iostream>
#include <unistd.h> // getopt   
#include "MrpcConfig.h"

MrpcConfig MrpcApplication::config;

MrpcApplication &MrpcApplication::GetInstance(){
    static MrpcApplication instance; //局部静态变量，线程安全
    return instance;
}

void showArgsHelp(){
    std::cout<<"Usage: ./mrpc_app -i <config_file_path>"<<std::endl;
    std::cout<<"Example: ./mrpc_app -i ./conf/mrpc.conf"<<std::endl;
}
void MrpcApplication::Init(int argc, char ** argv){
    // ip = "user";//使用静态成员变量，必须类外定义并初始化
    if(argc<2){
        showArgsHelp();
        exit(EXIT_FAILURE);
    }
    int c=0;
    std::string config_file;
    while((c = getopt(argc,argv,"i:") )!= -1){
        switch(c){
            case 'i':
                config_file=optarg;
                break;
            case '?':
                std::cout<<"Unknown option: "<<char(optopt)<<std::endl;
                showArgsHelp();
                exit(EXIT_FAILURE);
            case ':':
                std::cout<<"Option -"<<char(optopt)<<" requires an argument."<<std::endl;
                showArgsHelp();
                exit(EXIT_FAILURE);
            default:
                break;
        }
    }
    //读取配置文件
    config.LoadConfigFile(config_file.c_str());
    std::cout<<"rpcserverip: "<<config.Load("rpcserverip")<<std::endl;
    std::cout<<"rpcserverport: "<<config.Load("rpcserverport")<<std::endl;
    std::cout<<"zookeeperip: "<<config.Load("zookeeperip")<<std::endl;
    std::cout<<"zookeeperport: "<<config.Load("zookeeperport")<<std::endl;
}

MrpcConfig &MrpcApplication::GetConfig(){ 
    return config;
}